﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using YouErYuanModels;
using CommonLib;
using System.Data.SqlClient;

namespace YouErYuanBLL
{
    public class SchoolBLL
    {

        /// <summary>
        /// 更新幼儿园的名称，简介
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateSchool(School model)
        {
            string sql = "UPDATE [dbo].[T_School] SET [schoolIntroduce] = @schoolIntroduce,[schoolName] = @schoolName WHERE [schoolId] = @schoolId";
            SqlParameter[] parmas = {
                new SqlParameter("@schoolId",model.Schoolid),
                new SqlParameter("@schoolName",model.Schoolname),
                new SqlParameter("@schoolIntroduce",model.Schoolintroduce),
            };
            int count = SQLHepler.ExecuteSql(sql, parmas);
            return count;
        }

        /// <summary>
        /// 查询校园简介
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public object getSchoolInfo(School model)
        {
            string sql = "select schoolIntroduce,schoolName from t_school where schoolId=@schoolId";
            SqlParameter[] par = {
                new SqlParameter("@schoolId",model.Schoolid)
            };
            return SQLHepler.GetDataTable(sql, par);
        }

        /// <summary>
        /// 分页查询幼儿园记录
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="youErYuanName">幼儿园id或名称</param>
        /// <param name="total">条件查询总记录数</param>
        /// <returns></returns>
        public object GetSchoolPageList(int pageIndex, int pageSize, string youErYuanName, out int total)
        {
            List<School> list = new List<School>();
            string where = "";
            if (string.IsNullOrEmpty(youErYuanName) == false)
            {
                youErYuanName = SqlFilter.Filter(youErYuanName);
                where = "[schoolName] like '%" + youErYuanName + "%' or [schoolId]='"+ youErYuanName + "'";
            }
            DataTable dt = SQLHepler.GetPaging("[qvEndDate] desc", "[View_schoolList]", where, pageIndex, pageSize, out total);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var item = dt.Rows[i];
                    string password = item["password"].GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["password"]);
                    if (string.IsNullOrEmpty(password) == false)
                    {
                        string pwd = CommonLib.DESencodingTxt.DESDecoding(password);
                        dt.Rows[i]["password"] = pwd;//修改数据
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 添加幼儿园
        /// </summary>
        /// <param name="schoolName">幼儿园名称</param>
        /// <returns></returns>
        public int AddSchool(string schoolName)
        {
            string sql = "INSERT INTO [dbo].[T_School]([schoolId],[schoolName],[creatTime]) VALUES(@schoolId,@schoolName,@creatTime)";
            SqlParameter[] parames = {
                new SqlParameter("@schoolId",Guid.NewGuid().ToString()),
                new SqlParameter("@schoolName",schoolName),
                new SqlParameter("@creatTime",DateTime.Now)
            };
            return SQLHepler.ExecuteSql(sql, parames);
        }

        /// <summary>
        /// 修改幼儿园名称
        /// </summary>
        /// <param name="schoolId">幼儿园id</param>
        /// <param name="schoolName">幼儿园名称</param>
        /// <returns></returns>
        public int EditSchoolName(string schoolId,string schoolName)
        {
            string sql = "update [dbo].[T_School] set [schoolName]=@schoolName where [schoolId]=@schoolId";
            SqlParameter[] parames = { new SqlParameter("@schoolId", schoolId),new SqlParameter("@schoolName", schoolName) };
            return SQLHepler.ExecuteSql(sql, parames);
        }

        /// <summary>
        /// 设置幼儿园的查看签到截止日期，视频截止日期
        /// </summary>
        /// <param name="qvEndDate">签到截止日期、视频截止日期</param>
        /// <param name="schoolId">幼儿园id</param>     
        /// <returns></returns>
        public int SettingSchool(string qvEndDate, string[] schoolIds)
        {
            List<object[]> cmdList = new List<object[]>();
            string sql = "UPDATE [dbo].[T_School] SET [qvEndDate] = @qvEndDate WHERE [schoolId] = @schoolId";
            foreach (string schoolId in schoolIds)
            {
                SqlParameter[] parames = {
                    new SqlParameter("@schoolId",schoolId),
                    new SqlParameter("@qvEndDate",qvEndDate)                   
                };
                object[] arr = new object[2];
                arr[0] = sql;
                arr[1] = parames;
                cmdList.Add(arr);
            }
            int count = SQLHepler.ExecSqlTransacation(cmdList);
            return count;
        }

        /// <summary>
        /// 设置园长 添加
        /// </summary>
        /// <param name="schoolId"></param>
        /// <param name="yzName"></param>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public int SettingSchoolAccountAdd(string schoolId, string yzName, string account, string password, string status)
        {
            string sql = "INSERT INTO [dbo].[T_Teacher]([id],[teacherName],[password],[phone],[schoolId],[type],[status]) VALUES(@id,@teacherName,@password,@phone,@schoolId,@type,@status)";
            string pwd = CommonLib.DESencodingTxt.DESencrypt(password);
            SqlParameter[] parames = {
                new SqlParameter("@id",Guid.NewGuid().ToString()),
                new SqlParameter("@teacherName",yzName),
                new SqlParameter("@password",pwd),
                new SqlParameter("@phone",account),
                new SqlParameter("@schoolId",schoolId),
                new SqlParameter("@type","2"),
                new SqlParameter("@status",status)
            };
            int count = SQLHepler.ExecuteSql(sql, parames);
            return count;
        }

        /// <summary>
        /// 设置园长 修改园长账号，姓名，密码
        /// </summary>
        /// <param name="teacherId"></param>
        /// <param name="yzName"></param>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public int SettingSchoolAccountUpdate(string teacherId, string yzName, string account, string password, string status)
        {
            string sql = "UPDATE [dbo].[T_Teacher] SET [teacherName] = @teacherName,[password] = @password,[phone]=@phone,[status] =@status WHERE [id] = @id";
            string pwd = CommonLib.DESencodingTxt.DESencrypt(password);
            SqlParameter[] parames = {
                new SqlParameter("@teacherName",yzName),
                new SqlParameter("@password",pwd),
                new SqlParameter("@phone",account),
                new SqlParameter("@id",teacherId),
                new SqlParameter("@status",status)
            };
            int count = SQLHepler.ExecuteSql(sql, parames);
            return count;
        }

        /// <summary>
        /// 批量设置幼儿园签到消息模板
        /// </summary>
        /// <param name="schoolIds">幼儿园id数组</param>
        /// <param name="qiandaomuban">签到模板字符串</param>
        /// <returns></returns>
        public int UpdateSchoolQianDaoMuBan(string[] schoolIds, string qiandaomuban)
        {
            string sql = "UPDATE [dbo].[T_School] SET [qiaoDaoMuBan] = @qiaoDaoMuBan WHERE [schoolId] = @schoolId";
            List<object[]> cmdlist = new List<object[]>();
            foreach (string schoolId in schoolIds)
            {
                SqlParameter[] parames = { new SqlParameter("@schoolId", schoolId), new SqlParameter("@qiaoDaoMuBan", qiandaomuban) };
                object[] arr = new object[2];
                arr[0] = sql;
                arr[1] = parames;
                cmdlist.Add(arr);
            }
            return SQLHepler.ExecSqlTransacation(cmdlist);
        }

        /// <summary>
        /// 根据学校id查询信息
        /// </summary>
        /// <param name="schoolId"></param>
        /// <returns></returns>
        public School GetSchoolById(string schoolId)
        {
            string sql = "SELECT [schoolId],[schoolIntroduce],[schoolName],[creatTime],[qvEndDate],[qiaoDaoMuBan] FROM [dbo].[T_School] where [schoolId]=@schoolId ";
            DataTable dt = SQLHepler.GetDataSet(sql, new SqlParameter("@schoolId", schoolId)).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                School model = new School();
                var item = dt.Rows[0];
                model.Schoolid = item["schoolId"].GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["schoolId"]);
                model.Schoolintroduce = item["schoolIntroduce"].GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["schoolIntroduce"]);
                model.Schoolname = item["schoolName"].GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["schoolName"]);
                model.CreatTime = item["creatTime"].GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["creatTime"]);
                model.QvEndDate = item["qvEndDate"].GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["qvEndDate"]);
                model.QiaoDaoMuBan = item["qiaoDaoMuBan"].GetType() == typeof(System.DBNull) ? "" : Convert.ToString(item["qiaoDaoMuBan"]);
                return model;
            }
            return null;
        }


    }
}
